﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DCSLibrary;
using Utils.DC.Web.UI;
using Telerik.Web.UI;
using Utils.DatabaseUtils;

public partial class Admin_Module_quan_tri_thong_tin_san_pham : System.Web.UI.UserControl
{
    #region Cac bien dung chung

    ProductUI objProduct = new ProductUI();
    DataSet dsProduct;

    #endregion

    #region Cac Ham Su Kien

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindCategory();
            BindGrid();
        }
    }

    protected void Ajax_Request(object sender, AjaxRequestEventArgs e)
    {
        if (e.Argument == "Rebind")
        {
            BindGrid();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        
        BindGrid();
    }

    protected void Product_Delete(object sender, EventArgs e)
    {
        try
        {
            LinkButton button = (LinkButton)sender;
            int result = objProduct.DeleteByID(Convert.ToInt32(button.CommandArgument));
            if (result > 0)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_DELETE_SUCCESSFULLY;
                BindGrid();
            }
            else
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_DELETE_NOT_SUCCESSFULLY;
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
        }
    }

    #endregion

    #region Cac Ham Tuong Tac Du Lieu

    private void BindGrid()
    {
        try
        {
            /*dsProduct = objProduct.Search(txtProductName.Text.Trim(), Convert.ToInt32(cboCategory.SelectedValue), txtFromCurrentPrice.Value.HasValue ? txtFromCurrentPrice.Value.Value : 0,
                txtToCurrentPrice.Value.HasValue ? txtToCurrentPrice.Value.Value : 0,
                txtFromDiscountPrice.Value.HasValue ? txtFromDiscountPrice.Value.Value : 0,
                txtToDiscountPrice.Value.HasValue ? txtToDiscountPrice.Value.Value : 0);*/
            dsProduct = objProduct.GetAll();
            grd.DataSource = dsProduct.Tables[0];
            grd.DataBind();
        }
        catch (Exception e)
        {

        }
    }

    private void BindCategory()
    {
        FunctionPageUI obj = new FunctionPageUI();
        DataSet dsCategoryPage = obj.GetAll();
        try
        {
            cboCategory.Items.Clear();

            DataTable dt = new DataTable();
            dt.Columns.Add("FunctionPageID");
            dt.Columns.Add("FunctionPageName");

            DataTable dtParent = TableLib.CreateTable(dsCategoryPage.Tables[0], "ParentID = 4");
            DataTable dtChild = null;
            cboCategory.Items.Add(new ListItem("Tất cả", "0"));
            foreach (DataRow dbrow in dtParent.Rows)
            {
                cboCategory.Items.Add(new ListItem(dbrow["FunctionPageName"].ToString().Trim(), dbrow["FunctionPageID"].ToString().Trim()));
                dtChild = TableLib.CreateTable(dsCategoryPage.Tables[0], "ParentID = " + dbrow["FunctionPageID"].ToString().Trim());
                foreach (DataRow dbrow2 in dtChild.Rows)
                {
                    cboCategory.Items.Add(new ListItem("       -------" + dbrow2["FunctionPageName"].ToString().Trim(), dbrow2["FunctionPageID"].ToString().Trim()));
                    DataTable dtChildOfChild = TableLib.CreateTable(dsCategoryPage.Tables[0], "ParentID = " + dbrow2["FunctionPageID"].ToString().Trim() + " and IsAdmin = 0");
                    if (dtChildOfChild != null && dtChildOfChild.Rows.Count > 0)
                    {
                        foreach (DataRow dbrow3 in dtChildOfChild.Rows)
                        {
                            cboCategory.Items.Add(new ListItem("       --------------" + dbrow3["FunctionPageName"].ToString().Trim(), dbrow3["FunctionPageID"].ToString().Trim()));
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {

        }
    }

    protected void grd_OnPageIndexChanged(object source, GridPageChangedEventArgs e)
    {
        BindGrid();
    }

    #endregion
}
